<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------

namespace plugins\dingding\controller;

use cmf\controller\PluginAdminBaseController;
use plugins\dingding\model\PluginDingdingAppModel;
use think\Db;

class AdminIndexController extends PluginAdminBaseController
{
    protected function initialize()
    {
        parent::initialize();
        $adminId = cmf_get_current_admin_id(); //获取后台管理员id，可判断是否登录
        if (!empty($adminId)) {
            $this->assign('admin_id', $adminId);
        }
    }

    public function index()
    {
        $apps = Db::name('plugin_dingding_app')->select();
        $this->assign('apps', $apps);

        return $this->fetch('/admin_index');
    }

    public function Add()
    {
        return $this->fetch('/admin_add');
    }

    public function AddPost()
    {
        if ($this->request->isPost()) {
            $data   = $this->request->param();
            //$result = $this->validate($data, 'app');
            $result = true;
            if ($result !== true) {
                // 验证失败 输出错误信息
                $this->error($result);
            } else {
                $app_model = new PluginDingdingAppModel();
                $result = $app_model->allowField(true)->save($data);
                if ($result) {
                    $this->success("添加应用成功", cmf_plugin_url("dingding://AdminIndex/index"));
                } else {
                    $this->error("添加应用失败");
                }
            }
        }
    }
    public function Edit()
    {
        $id = $this->request->param("id", 0, 'intval');

        $data = Db::name('plugin_dingding_app')->where("id", $id)->find();
        if (!$data) {
            $this->error("该应用不存在！");
        }
        $this->assign("data", $data);
        return $this->fetch('/admin_edit');
    }

    public function EditPost()
    {
        $id = $this->request->param("id", 0, 'intval');

        if ($this->request->isPost()) {
            $data   = $this->request->param();
            //$result = $this->validate($data, 'app');
            $result = true;
            if ($result !== true) {
                // 验证失败 输出错误信息
                $this->error($result);

            } else {
                $app_model = new PluginDingdingAppModel();
                if ($app_model->allowField(true)->isUpdate(true)->save($data) !== false) {
                    $this->success("保存成功！", cmf_plugin_url('dingding://AdminIndex/index'));
                } else {
                    $this->error("保存失败！");
                }
            }
        }
    }
    public function Delete()
    {
        $id = $this->request->param("id", 0, 'intval');

            $status = Db::name('plugin_dingding_app')->delete($id);
            if (!empty($status)) {
                $this->success("删除成功！", cmf_plugin_url('dingding://AdminIndex/index'));
            } else {
                $this->error("删除失败！");
            }

    }

    public function setting()
    {
        $users = Db::name('user')->limit(0, 5)->select();
        //$demos = PluginDemoModel::all();

        // print_r($demos);

        $this->assign('users', $users);

        $this->assign('users', $users);

        return $this->fetch('/admin_index');
    }

    public function sense(){
        $apps = Db::name('plugin_dingding_sense')->select();
        $this->assign('apps', $apps);

        return $this->fetch('/admin_sense');
    }
}
